// codecvt facet for EUC multibyte code, JIS-X0208 wide-character code
#pragma once
#ifndef _CVT_EUC_0208_
#define _CVT_EUC_0208_
#ifndef RC_INVOKED
#include <cwchar>
#include <locale>

#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new

namespace stdext {
    namespace cvt {


        using _Statype = _CSTD mbstate_t;

        // CLASS TEMPLATE codecvt_euc_0208
        template <class _Elem, unsigned long _Maxcode = 0x7e7e>
        class codecvt_euc_0208
            : public _STD codecvt<_Elem, char, _Statype> { // facet for converting between JIS-X0208 _Elem and EUC bytes
        public:
            using _Mybase     = _STD codecvt<_Elem, char, _Statype>;
            using result      = typename _Mybase::result;
            using _Byte       = char;
            using intern_type = _Elem;
            using extern_type = _Byte;
            using state_type  = _Statype;

            explicit codecvt_euc_0208(size_t _Refs = 0) : _Mybase(_Refs) { // construct with ref count
            }

            virtual ~codecvt_euc_0208() noexcept { // destroy the object
            }

        protected:
            virtual result do_in(_Statype&, const _Byte* _First1, const _Byte* _Last1, const _Byte*& _Mid1,
                _Elem* _First2, _Elem* _Last2,
                _Elem*& _Mid2) const { // convert bytes [_First1, _Last1) to [_First2, _Last)
                _Mid1 = _First1;
                _Mid2 = _First2;

                while (_Mid1 != _Last1 && _Mid2 != _Last2) { // convert a multibyte sequence
                    unsigned char _By = (unsigned char) *_Mid1;
                    unsigned short _Ch;
                    bool _Extra;

                    if (_By == 0x8e) {
                        _Ch    = 0;
                        _Extra = true;
                    } else if (0xa1 <= _By && _By <= 0xfe) {
                        _Ch    = (unsigned short) ((_By << 8) - 0x8080);
                        _Extra = true;
                    } else {
                        _Ch    = _By;
                        _Extra = false;
                    }

                    if (_Extra) {
                        if (_Last1 - _Mid1 < 2) {
                            break;
                        }

                        if ((_By = (unsigned char) *++_Mid1) < 0xa1 || 0xfe < _By) {
                            return _Mybase::error; // bad second byte
                        }

                        _Ch = (unsigned short) (_Ch + _By);
                    }

                    if (_Maxcode < _Ch) {
                        return _Mybase::error; // code too large
                    }

                    ++_Mid1;
                    *_Mid2++ = (_Elem) _Ch;
                }

                return _First1 == _Mid1 ? _Mybase::partial : _Mybase::ok;
            }

            virtual result do_out(_Statype&, const _Elem* _First1, const _Elem* _Last1, const _Elem*& _Mid1,
                _Byte* _First2, _Byte* _Last2,
                _Byte*& _Mid2) const { // convert [_First1, _Last1) to bytes [_First2, _Last)
                _Mid1 = _First1;
                _Mid2 = _First2;

                while (_Mid1 != _Last1 && _Mid2 != _Last2) { // convert and put a wide char
                    unsigned long _Ch = (unsigned long) *_Mid1;

                    if (_Maxcode < _Ch) {
                        return _Mybase::error;
                    }

                    if (0xa1 <= _Ch && _Ch <= 0xfe) {
                        if (_Last2 - _Mid2 < 2) {
                            break;
                        }

                        // put 0x8e plus 1-byte code
                        *_Mid2++ = (_Byte)(unsigned short) 0x8e;
                        *_Mid2++ = (_Byte) _Ch;
                    } else if (_Ch == 0x8e) {
                        return _Mybase::error;
                    } else if (_Ch <= 0xff) {
                        *_Mid2++ = (_Byte) _Ch;
                    } else {
                        if (_Last2 - _Mid2 < 2) {
                            break;
                        }

                        // put 2-byte code
                        unsigned char _By = (unsigned char) ((_Ch >> 8) + 0x80);

                        if (_By < 0xa1 || 0xfe < _By) {
                            return _Mybase::error; // bad first byte
                        }

                        *_Mid2++ = (_Byte) _By;

                        _By = (unsigned char) (_Ch + 0x80);
                        if (_By < 0xa1 || 0xfe < _By) {
                            return _Mybase::error; // bad second byte
                        }

                        *_Mid2++ = (_Byte) _By;
                    }

                    ++_Mid1;
                }

                return _First1 == _Mid1 ? _Mybase::partial : _Mybase::ok;
            }

            virtual result do_unshift(_Statype&, _Byte* _First2, _Byte*,
                _Byte*& _Mid2) const { // generate bytes to return to default shift state
                _Mid2 = _First2;

                return _Mybase::ok;
            }

            virtual int do_length(_Statype& _State, const _Byte* _First1, const _Byte* _Last1, size_t _Count) const
                noexcept { // return min(_Count, converted length of bytes [_First1, _Last1))
                size_t _Wchars    = 0;
                _Statype _Mystate = _State;

                while (_Wchars < _Count && _First1 != _Last1) { // convert another wide character
                    const _Byte* _Mid1;
                    _Elem* _Mid2;
                    _Elem _Ch;

                    switch (do_in(_Mystate, _First1, _Last1, _Mid1, &_Ch, &_Ch + 1,
                        _Mid2)) { // test result of single wide-char conversion
                    case _Mybase::noconv:
                        return (int) (_Wchars + (_Last1 - _First1));

                    case _Mybase::ok:
                        if (_Mid2 == &_Ch + 1) {
                            ++_Wchars; // replacement do_in might not convert one
                        }

                        _First1 = _Mid1;
                        break;

                    default:
                        return (int) _Wchars; // error or partial
                    }
                }

                return (int) _Wchars;
            }

            virtual bool do_always_noconv() const noexcept { // return true if conversions never change input
                return false;
            }

            virtual int do_max_length() const noexcept { // return maximum length required for a conversion
                return 3;
            }

            virtual int do_encoding() const noexcept { // return length of code sequence (from codecvt)
                return 0; // 0 => varying length
            }
        };
    } // namespace cvt
} // namespace stdext

#ifdef _TEST_IT
#define NCHARS 0x10000
#define MYWC_MAX 0x7e7e
#define MYFILE "euc_0208"
#define MYNAME stdext::cvt::codecvt_euc_0208<Mywchar>

#include <cvt/xtest>
#endif // _TEST_IT

#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
#pragma warning(pop)
#pragma pack(pop)

#endif // RC_INVOKED
#endif // _CVT_EUC_0208_

/*
 * Copyright (c) by P.J. Plauger. All rights reserved.
 * Consult your license regarding permissions and restrictions.
V6.50:0009 */
